package cn.gzsxt.music.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

import cn.gzsxt.music.query.PowerQueryObject;
@Mapper
public interface PowerMapper {
	/**
	 * 根据条件查询结果
	 * 
	 * @param qo 条件对象
	 * @return 结果集
	 */
	@Select("select p.*,m.modular_name from tb_Power p , tb_modular m where p.modular_id = m.modular_id limit #{beginIndex},#{rows}")
	List<Map<String, Object>> selectPowerListByCondition(PowerQueryObject qo);

	/**
	 * 根据条件查询总数
	 * 
	 * @param qo 条件对象
	 * @return 总数
	 */
	@Select("select count(*) from tb_Power p , tb_modular m where p.modular_id = m.modular_id")
	Long selectPowerTotalByCondition(PowerQueryObject qo);

	/**
	 * 插入操作
	 * 
	 * @param params
	 * @return
	 */
	@Insert("insert into tb_Power (Power_name,power_action,power_display,power_parent_id,modular_id)"
			+ " values (#{power_name},#{power_action},#{power_display},#{power_parent_id},#{modular_id})")
	// @Options(useGeneratedKeys=true,keyProperty="Power_id")
	int insert(Map<String, Object> params);

	/**
	 * 删除指定id的模块
	 * 
	 * @param Power_id
	 * @return
	 */
	@Delete("delete from tb_power where power_id = #{power_id}")
	int deletePowerById(@Param("power_id") Long power_id);

	/**
	 * 	修改指定id的模块名称
	 * @param params
	 * @return
	 */
	@Update("update tb_power set power_name = #{power_name},power_action=#{power_action},power_display=#{power_display}"
			+ ",power_parent_id=#{power_parent_id},modular_id=#{modular_id} where power_id = #{power_id}")
	int update(Map<String, Object> params);
	
	/**
	 * 根据power_action 查询数据表中是否有此action,有返回对应的map集合,没有返回null
	 * @param params
	 * @return
	 */
	//@Select("select * from tb_power where power_action = #{power_action} and power_id !=#{power_id}")
	//执行 PowerProvider 的动态sql语句
	@SelectProvider(type=PowerProvider.class,method="checkExistsPowerActionName")
	Map<String,Object> checkExistsPwerAction(Map<String,Object> params);
	
	@SelectProvider(type=PowerProvider.class,method="getPowerByPowerActions")
	List<Map<String, Object>> getPowerByPowerActions(String role_action);
	
	/**
	 * 根据模块id查询对应权限
	 * @param modular_id 模块id
	 * @return 权限集合
	 */
	@Select("select power_id id,power_name text,power_action from tb_power where modular_id = #{modular_id} and power_display = 0")
	List<Map<String, Object>> selectPowersByModularId(@Param("modular_id") Long modular_id);
	
	/**
	 * 获取所有的权限,以供权限管理的时候使用
	 * @return
	 */
	@Select("select * from tb_power")
	List<Map<String, Object>> getAllPowers();
	
	
}

